import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index',
    component: () => import('../views/Main.vue'),
    children: [
      {
        path: '/index',
        component: () => import('../views/Index.vue')
      },
      {
        path: '/article',
        component: () => import('../views/Article.vue')
      },
      {
        path: '/book',
        component: () => import('../views/Book.vue')
      },
      {
        path: '/diary',
        component: () => import('../views/Diary.vue')
      },
      {
        path: '/articleDetail/:id',
        component: () => import('../views/ArticleDetail.vue')
      },
      {
        path: '/about',
        component: () => import('../views/About.vue')
      },
      {
        path: '/message',
        component: () => import('../views/Message.vue')
      },
      {
        path: '/friend',
        component: () => import('../views/FriendBlog.vue')
      },
      // 我的主页
      {
        path: '/myIndex',
        redirect: '/myIndex/userInfo',
        component: () => import('../views/user/MyIndex.vue'),
        meta: { requireLogin: true },
        children: [
          {
            path: '/myIndex/userInfo',
            component: () => import('../views/user/UserInfo.vue'),
            meta: { requireLogin: true }
          },
          {
            path: '/myIndex/updatePwd',
            component: () => import('../views/user/UpdatePwd.vue'),
            meta: { requireLogin: true }
          },
          {
            path: '/myIndex/myInform',
            component: () => import('../views/user/MyInform.vue'),
            meta: { requireLogin: true }
          },
          {
            path: '/myIndex/myAdvice',
            component: () => import('../views/user/MyAdvice.vue'),
            meta: { requireLogin: true }
          },
          {
            path: '/myIndex/myBug',
            component: () => import('../views/user/MyBug.vue'),
            meta: { requireLogin: true }
          },
          {
            path: '/myIndex/myFriendBlog',
            component: () => import('../views/user/MyFriendBlog.vue'),
            meta: { requireLogin: true }
          }
        ]
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // 我的主页路由需要登录
  if (to.meta.requireLogin) {
    const token = window.localStorage.getItem('lhamster_identity_info')
    if (!token) {
      return next('/')
    }
  }
  // 不需要登录的路由
  next()
})
export default router
